//
//  UIResourceTools.m
//  XMPPClient
//
//  Created by zhihuiguan on 14-4-10.
//  Copyright (c) 2014年 zhihuiguan. All rights reserved.
//

#import "UIResourceTools.h"

#define kDefaultColor [UIColor clearColor]
#define kResourceConfigFileName @"commonConfig.plist"
#define kCustResourceConfigFileName @"default.bundle/configInfo.plist"


CG_INLINE id NSDictionaryObjectForKey(NSDictionary *dict, NSString *key, id defaultObj)
{
    if (!dict || !key) {
        return defaultObj;
    }
    
    id obj = [dict objectForKey:key];
    
    return (obj ? obj : defaultObj);
}


static NSDictionary *_configDict = nil;
static NSDictionary *_custConfigDict = nil;

@implementation UIResourceTools

#pragma mark - Config Loading

+(void)initialize
{
    [self loadConfigurationFile];
}

+(void)loadConfigurationFile
{
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:kResourceConfigFileName ofType:nil ];
    _configDict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
    
    
    plistPath = [[NSBundle mainBundle] pathForResource:kCustResourceConfigFileName ofType:nil ];
    _custConfigDict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
    
}

+(id)getCfg:(NSString *)key
{
    if (![_custConfigDict objectForKey:key]) {
        return [_configDict objectForKey:key];
    }
    return [_custConfigDict objectForKey:key];
}
+(UIColor *)colorForKey:(NSString *)key
{
    return [self colorForKey:key defaultColor:kDefaultColor];
}

+(UIColor *)colorForKey:(NSString *)key defaultColor:(UIColor *)defalutColor
{
    NSDictionary *_colorTableDict = [_custConfigDict objectForKey:@"ColorTable"];
    
    NSString *colorString = nil;
    if (_colorTableDict) {
        colorString = [_colorTableDict objectForKey:key];
    }
    
    if (!colorString) {
        
        _colorTableDict = [_configDict objectForKey:@"ColorTable"];
        
        if (_colorTableDict) {
            colorString = [_colorTableDict objectForKey:key];
        }
        
    }
    
    UIColor *color = nil;
    if (colorString) {
        color = [self colorWithHexString:colorString];
    }
    
    return (color ? color : defalutColor);
}

+(UIColor *)colorWithHexString:(NSString *)color
{
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return nil;
    }
    
    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"])
    {
        cString = [cString substringFromIndex:2];
    }
    
    if ([cString hasPrefix:@"#"])
    {
        cString = [cString substringFromIndex:1];
    }
    
    if ([cString length] != 6 && [cString length] != 8)
    {
        return nil;
    }
    
    // Separate into r, g, b, a substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    
    //r
    NSString *rString = [cString substringWithRange:range];
    
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    unsigned int a = 255;
    if ([cString length] == 8) {
        range.location = 6;
        NSString *aString = [cString substringWithRange:range];
        [[NSScanner scannerWithString:aString] scanHexInt:&a];
    }
    
    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:((float) a / 255.0f)];
}

#pragma mark Config Loading
#pragma mark -


#pragma mark - Config Description

////////////////////////////Key Definition////////////////////////////
#define kStatusBarStyle @"kStatusBarStyle"
#define kStatusBarStyleInLogin @"kStatusBarStyleInLogin"


#define kThemeColor @"kThemeColor"


#define kNavigationBarTitleColor @"kNavigationBarTitleColor"
#define kNavigationBarTintColorNormal  @"kNavigationBarTintColorNormal"

#define kBarButtonItemTitleColorNormal @"kBarButtonItemTitleColorNormal"
#define kBarButtonItemTintColor @"kBarButtonItemTintColor"


#define kTabBarItemTitleColorNormal @"kTabBarItemTitleColorNormal"
#define kTabBarItemTitleColorHighlighted @"kTabBarItemTitleColorHighlighted"


#define kSwitchOnTintColor @"kSwitchOnTintColor"
#define kSwitchTintColor @"kSwitchTintColor"
#define kSwitchThumbTintColor @"kSwitchThumbTintColor"


#define kTableViewHeaderViewBackgroundColor @"kTableViewHeaderViewBackgroundColor"


#define kSegmentedControlBackgroundColor @"kSegmentedControlBackgroundColor"
#define kSegmentedControlBorderColor @"kSegmentedControlBorderColor"
#define kSegmentedControlIndicatorColor @"kSegmentedControlIndicatorColor"
#define kSegmentedControlTitleColorNormal @"kSegmentedControlTitleColorNormal"
#define kSegmentedControlTitleColorHighlighted @"kSegmentedControlTitleColorHighlighted"

////////////////////////////Key Definition////////////////////////////

+(UIColor *)mainColor
{
    /*
    static UIColor *mainColor = nil;
    if (!mainColor) {
        mainColor = UIColorFromRGB(kMainColorValue);
    }
    return mainColor;
     */
    
    return [self colorForKey:kThemeColor defaultColor:[UIColor blueColor]];
}

#pragma mark - /////////////////////////////////

+(UIImage *)imageNamed:(NSString *)name
{
    return [UIImage imageNamed:name];
}

#pragma mark - /////////////////////////////////

+(UIColor*)navigationBarTitleColor
{
    //return [UIColor whiteColor];
    
    return [self colorForKey:kNavigationBarTitleColor defaultColor:[UIColor whiteColor]];
}

+(UIColor *)navigationBarTintColor
{
//    return [UIColor blackColor];
    return [self colorForKey:kNavigationBarTintColorNormal defaultColor:[UIColor blackColor]];
}

#pragma mark - /////////////////////////////////

+(UIColor *)barButtonItemTitleColor
{
    //return [UIColor whiteColor];
    
    return [self colorForKey:kBarButtonItemTitleColorNormal defaultColor:[UIColor whiteColor]];
}

+(UIColor *)barButtonItemTintColor
{
    //return [UIColor whiteColor];
    
    return [self colorForKey:kBarButtonItemTintColor defaultColor:[UIColor whiteColor]];
}

#pragma mark - /////////////////////////////////

+(UIStatusBarStyle)statusBarStyle
{
    //return UIStatusBarStyleBlackOpaque;
    
    NSNumber *n = NSDictionaryObjectForKey(_configDict, kStatusBarStyle, [NSNumber numberWithInteger:UIStatusBarStyleLightContent]);
    
    return [n integerValue];
}

+(UIStatusBarStyle)loginStatusBarStyle
{
    //return UIStatusBarStyleDefault;
    
    NSNumber *n = NSDictionaryObjectForKey(_configDict, kStatusBarStyleInLogin, [NSNumber numberWithInteger:UIStatusBarStyleDefault]);
    
    return [n integerValue];
}

#pragma mark - /////////////////////////////////

+(UIColor *)tabBarItemTitleColorHighlighted
{
    //return [UIColor whiteColor];
    
    return [self colorForKey:kTabBarItemTitleColorHighlighted defaultColor:[UIColor whiteColor]];
}

//+(UIColor *)tabBarItemTitleColorNormal
//{
//    //return UIColorFromRGB(0x989898);
//    
//    return [self colorForKey:kTabBarItemTitleColorNormal defaultColor:UIColorFromRGB(0x989898)];
//}

#pragma mark - /////////////////////////////////

+(UIColor *)switchOnTintColor
{
//    UIColor *color = [UIColor colorWithRed:60/255 green:60/255 blue:60/255 alpha:0.6];
//    return color;
    
//    return nil;
    
    return [self colorForKey:kSwitchOnTintColor defaultColor:nil];
}

+(UIColor *)switchTintColor
{
    return [self colorForKey:kSwitchTintColor defaultColor:nil];
}

+(UIColor *)switchThumbTintColor
{
    return [self colorForKey:kSwitchThumbTintColor defaultColor:nil];
}

#pragma mark - /////////////////////////////////

+(UIColor *)tableViewHeaderViewBackgroundColor
{
    //0x3C3C3C99
//    UIColor *color = [UIColor colorWithRed:60/255 green:60/255 blue:60/255 alpha:0.6];
//    return color;
    
    return [self colorForKey:kTableViewHeaderViewBackgroundColor defaultColor:[UIColor colorWithRed:60/255 green:60/255 blue:60/255 alpha:0.6]];
}

#pragma mark - /////////////////////////////////

+(UIColor *)pureButtonColor
{
//    UIColor *color = UIColorFromRGB(0x99CA37);
//    return color;

    return [self mainColor];
}

//TODO
+(UIColor *)segmentColor
{
#ifdef BUILD_FOR_ST
    UIColor *color = [UIColor colorWithRed:60/255 green:60/255 blue:60/255 alpha:0.6];
    return color;
#else
//    UIColor *color = [UIColor colorWithRed:60/255 green:60/255 blue:60/255 alpha:0.6];
    return [self mainColor];
#endif

}

+(UIColor *)segmentedControlBackgroundColor
{
    return [self colorForKey:kSegmentedControlBackgroundColor defaultColor:nil];
}

+(UIColor *)segmentedControlBorderColor
{
    return [self colorForKey:kSegmentedControlBorderColor defaultColor:nil];
}

+(UIColor *)segmentedControlIndicatorColor
{
    return [self colorForKey:kSegmentedControlIndicatorColor defaultColor:nil];
}

+(UIColor *)segmentedControlTitleColorNormal
{
    return [self colorForKey:kSegmentedControlTitleColorNormal defaultColor:nil];
}

+(UIColor *)segmentedControlTitleColorHighlighted
{
    return [self colorForKey:kSegmentedControlTitleColorHighlighted defaultColor:nil];
}

#pragma mark Config Description
#pragma mark -

@end
